package com.hao.wms.mvc;

import com.hao.wms.domain.Employee;
import com.hao.wms.query.EmployeeQO;
import com.hao.wms.service.IDepartmentService;
import com.hao.wms.service.IEmployeeService;
import com.hao.wms.service.IRoleService;
import com.hao.wms.util.MD5;
import com.hao.wms.util.RequiredPermission;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IEmployeeService employeeService;
	private IDepartmentService departmentService;
	private IRoleService roleService;
	private Employee employee = new Employee();
	private List<Long> ids = new ArrayList<>();

	public void setIds(List<Long> ids) {
		this.ids = ids;
	}

	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	private EmployeeQO queryObject = new EmployeeQO();
	

	public EmployeeQO getQueryObject() {
		return queryObject;
	}
	public void setQueryObject(EmployeeQO queryObject) {
		this.queryObject = queryObject;
	}
	
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	public void setDepartmentService(IDepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	@RequiredPermission(value="员工列表")
	@InputConfig(methodName = "input")
	public String execute() {
		try {
			putContext(PAGERESULT, employeeService.query(queryObject));
			putContext("depts", departmentService.list());
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return LIST;
	}
	@RequiredPermission(value="新增/修改员工")
	public String input() {
		putContext("roles", roleService.list());
		putContext("depts", departmentService.list());
		if(employee.getId()!=null) {
			employee = employeeService.get(employee.getId());
		}
		return INPUT;
	}
	@RequiredPermission(value="修改员工")
	public String save() {
		try {
			if(employee.getId()!=null) {
                employeeService.update(employee);
            }else {
                employee.setPassword(MD5.encode(employee.getPassword()));
                employeeService.save(employee);
                addActionMessage("保存成功！");
            }
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	public void prepareSave(){
		if(employee.getId()!=null) {
			employee = employeeService.get(employee.getId());
			employee.setDept(null);
		}
		employee.getRoles().clear();
	}
	@RequiredPermission(value="删除员工")
	public String delete() throws IOException {
		employeeService.delete(employee);
		putResponseText("删除成功");
		return NONE;
	}
	@RequiredPermission(value="批量删除")
	public String batchDelete() {
		employeeService.batchDelete(ids);
		return NONE;
	}
	
	

}
